# encoding    : utf-8
# author      : 伦裕如
# project     : auto
# file        : login
# time        : 2024-06-27 下午4:49
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


def login(username: str, password: str):
    # 获取浏览器驱动
    driver = webdriver.Chrome()
    # 设置全局隐式等待
    wd = WebDriverWait(driver, 10)
    # 进入网址
    driver.get("http://localhost:90/")
    # 切换窗口到全屏
    driver.maximize_window()

    # 获取页面元素
    # 填写用户名
    driver.find_element(By.ID, "username").send_keys(username)
    # 填写密码
    driver.find_element(By.CSS_SELECTOR, "input[type='password']").send_keys(password)
    # time.sleep(20)
    # 点击登录 //*[@id="login_btn"]
    driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
    time.sleep(1)

    return driver


def login_err(num, username: str, password: str, expect):
    # 获取浏览器驱动
    driver = webdriver.Chrome()
    # 设置全局隐式等待
    # wd = WebDriverWait(driver, 10)
    # 进入网址
    driver.get("http://localhost:90/")
    # 切换窗口到全屏
    driver.maximize_window()

    # 获取页面元素
    # 填写用户名
    driver.find_element(By.ID, "username").send_keys(username)
    # 填写密码
    driver.find_element(By.CSS_SELECTOR, "input[type='password']").send_keys(password)
    # time.sleep(20)
    # 点击登录 //*[@id="login_btn"]
    driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
    time.sleep(1)

    # 获取页面标题用来判断结果
    help_block = driver.find_elements(By.CLASS_NAME, "help-block")
    texts = []
    for block in help_block:
        texts.append(block.text)
    if expect in texts:
        return True
    driver.save_screenshot(f"login_err_{num}.png")
    return False


def register(num: int, username: str, password: str, confirm_password: str, expect: str):
    """
    注册用户
    :param num: 测试编号
    :param username: 用户名
    :param password: 密码
    :param confirm_password: 确认密码
    :param expect: 期望结果
    :return:
    """

    # 获取浏览器驱动
    with webdriver.Chrome() as driver:
        # 前往指定网页
        driver.get("http://localhost:90")
        # 跳转到注册页面
        driver.find_element(By.PARTIAL_LINK_TEXT, "注册").click()
        time.sleep(0.5)

        # 填写注册信息注册用户
        driver.find_element(By.CSS_SELECTOR, "input#username").send_keys(username)
        driver.find_element(By.CSS_SELECTOR, "input#password").send_keys(password)
        driver.find_element(By.CSS_SELECTOR, "input#repassword").send_keys(confirm_password)
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(0.5)

        # 判断结果是否符华预期
        if driver.title == "度小满理财-登录[内测版]":
            if expect == driver.title:
                return True
            else:
                driver.save_screenshot(f"register_err_{num}.png")
        else:
            help_block = driver.find_elements(By.CLASS_NAME, "help-block")
            texts = []
            for block in help_block:
                texts.append(block.text)
            if expect in texts:
                return True
            else:
                driver.save_screenshot(f"register_err_{num}.png")
        return False


def operate_bank_card(driver: webdriver.Chrome, expect: str):
    driver.find_element(By.PARTIAL_LINK_TEXT, "个人中心").click()
    time.sleep(0.5)
    driver.find_element(By.PARTIAL_LINK_TEXT, "银行卡管理").click()
    time.sleep(0.5)

    # 记录当前银行卡
    cards = driver.find_elements(By.CLASS_NAME, "col-sm-12")
    num1 = len(cards)

    # 点击新增按钮
    driver.find_element(By.ID, "bankCard_add_modal_btn").click()
    time.sleep(0.5)
    driver.find_element(By.ID, "cardBank_add_input").send_keys("伊朗中央银行")
    driver.find_element(By.ID, "cardNum_add_input").send_keys("1234567891548753")
    time.sleep(0.5)
    driver.find_element(By.ID, "bankCard_save_btn").click()
    time.sleep(1)

    # 记录新增银行卡后银行卡
    num2 = len(driver.find_elements(By.CLASS_NAME, "col-sm-12"))
    return num1 + 1 == num2


def buy_products(num: int, driver: webdriver.Chrome, function: str, product: str, p_money, pay_password: str,
                 cancel: bool, expect):
    """
    购买产品 未完成
    :param expect: 预期结果
    :param num: 测试用例编号
    :param driver: 驱动对象
    :param function: 购买产品种类
    :param product: 购买产品
    :param p_money: 购买产品金额
    :param pay_password: 支付密码
    :param cancel: 取消交易
    :return:
    """
    money = get_balance(driver)
    driver.find_element(By.PARTIAL_LINK_TEXT, "个人理财").click()
    driver.find_element(By.PARTIAL_LINK_TEXT, function).click()
    driver.find_element(By.XPATH, f"buytnname={product}").click()

    # 输入密码
    driver.find_element(By.ID, "password").send_keys(pay_password)
    if cancel:
        # 点击取消
        driver.find_element(By.XPATH, "//div[@class='layui-layer-btn' and @class ='layui-layer-btn-']/a[1]").click()
    else:
        # 点击确认
        driver.find_element(By.XPATH, "//div[@class='layui-layer-btn layui-layer-btn-']/a[0]").click()
        text = driver.find_element(By.XPATH, "//div[@id=jconfirm-box80521]/div").text
        if expect != text:
            driver.save_screenshot(f"buy_products_err_{num}.png")
            return False
        else:
            if get_balance(driver) == money + p_money:
                return True
            driver.save_screenshot(f"buy_products_err_{num}.png")
            return False


def get_balance(driver: webdriver.Chrome):
    """
    前往银行卡页面查看银行卡余额
    :param driver: 驱动
    :return: 余额
    """
    # 页面跳转
    driver.find_element(By.PARTIAL_LINK_TEXT, "个人中心").click()
    time.sleep(0.5)
    driver.find_element(By.PARTIAL_LINK_TEXT, "银行卡管理").click()
    time.sleep(0.5)

    # 查找默认银行卡
    node = driver.find_element(By.XPATH, "//[text()='(默认)']")
    string = node.find_element(By.XPATH, "../../following-sibling::div/h4[1]").text
    # 取出金钱部分
    start, end = 0, 0
    for index, char in enumerate(string):
        if char.isdigit():
            if start == 0:
                start = index
            end = index + 1
    # 切换数据类型为浮点并返回
    money = string[start:end]
    return float(money)


